package szkeleton;

import java.awt.Point;
import java.io.File;

import szkeleton.Gamelogic.Direction;

enum YMovement
{
	fall, stand, jump;
}

public class Player implements MySerializable{
	private Point position;
	private Tile owner;
	private YMovement yMovement;
	private int jumpCounter;
	private Point pointToGo;
	private static final int maxJumpCounter = 2;
	private Tile newOwner;

	
	public Player(Tile owner, Point position){
		if(Controller.isDebug()) System.out.println("Player konstruktor");
		loadFromFile();
		this.owner = owner;
		this.newOwner = owner;
		yMovement = YMovement.stand;
		jumpCounter = 0;
		this.position = position;
	}
	
	public void setNewOwner(Tile newOwner)
	{
		this.newOwner = newOwner;
	}
	

	
	public Point getPosition(){
		return position;
	}
	public Tile getOwner()
	{
		return owner;
	}
	public void setPosition(Point p){
		position=p;
	}
	
	public void setPointToGo(Point p)
	{
		pointToGo=p;
	}
	public void move(Direction dir)
	{
		if(Controller.isDebug()) System.out.println("Player position: " + position);
		
		if (dir == Direction.north && yMovement != YMovement.fall) yMovement = YMovement.jump;
		
		pointToGo = Gamelogic.dirToPoint(dir);
		
		//ymovement
		if (yMovement != YMovement.stand)
		{
			if(yMovement == YMovement.jump)
			{
				pointToGo.y = -1;
				
				if(jumpCounter == maxJumpCounter) yMovement = YMovement.fall;
				else jumpCounter++;
			}
			if(yMovement == YMovement.fall)
			{
				pointToGo.y = 1;
			}
		}
		
		pointToGo.x += position.x;
		pointToGo.y += position.y;
		
		Element collider = owner.getElement(pointToGo,this);
		
		collider.collide(this);
		if(Controller.isDebug()) System.out.println("Player position after: " + position);
	}
	
	public void setTile(Tile newOwner)
	{
		if(Controller.isDebug()) System.out.println("Player.setTile");
		owner = newOwner;
	}
	
	

	public void blackCollided()
	{
		if(Controller.isDebug()) System.out.println("Player.blackCollided()");
		
		newOwner = owner;
		pointToGo = position;
		
		if(yMovement != YMovement.stand && !owner.isOnGap(position)) this.jumpCounter = 0;
		if(yMovement == YMovement.jump)	yMovement = YMovement.fall;
		if(yMovement == YMovement.fall)	yMovement = YMovement.stand;
		
	}
	
	public void whiteCollided()
	{
		if(Controller.isDebug()) System.out.println("Player.WhiteCollided()");
		
		position = pointToGo;
		owner = newOwner;
		
		if(yMovement == YMovement.stand && owner.isOnGap(pointToGo))
		{
			yMovement = YMovement.fall;
		}
	}

	public void died()
	{
		if(Controller.isDebug()) System.out.println("Player.died()");
		position = owner.getSpawnPoint();
	}
	
	public void loadFromFile() {
		if(Controller.isDebug()) System.out.println("Player.loadFromFile()");
		//kiolvassa a poziciojat a fajlbol (spawn-point, tile0)
		
	}

	public void writeToFile(File f) {
		if(Controller.isDebug()) System.out.println("Player.write2File()");
		//meg egy darabig nem lesz megirva
	}
	public void win()
	{	
		this.whiteCollided();
		Gamelogic.setWin(true);
		if (Controller.isDebug())
		if (Gamelogic.getPlayer(0).equals(this)) System.out.println("Player 0 win.");
		else System.out.println("Player 1 win.");
		
	}

}

